package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

class Demo extends JFrame
{
	public Demo()
	{
		this.setTitle("登录界面");
		this.setSize(300,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		JPanel panel=new JPanel(new FlowLayout(FlowLayout.CENTER,50,10));
		JMenuBar m=new JMenuBar();
		JMenu menu1=new JMenu("帮助");
		JMenu menu2=new JMenu("菜单");
		JMenuItem item=new JMenuItem("没有帮助");
		JMenuItem item1=new JMenuItem("没有菜单");
		menu1.add(item);
		menu2.add(item1);
		m.add(menu2);
		m.add(menu1);
		this.setJMenuBar(m);
		JLabel j=new JLabel("账号");
		j.setFont(new Font("楷体",Font.BOLD,20));
		j.setForeground(Color.red);
		JTextField field=new JTextField(15);
		field.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO 自动生成的方法存根
				char keychar=e.getKeyChar();
				System.out.print(keychar);
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO 自动生成的方法存根
				
			}
		});
		panel.add(j);
		panel.add(field);
		JLabel j1=new JLabel("密码");
		j1.setFont(new Font("楷体",Font.BOLD,20));
		j1.setForeground(Color.red);
		JPasswordField passwordField=new JPasswordField(15);
		passwordField.addKeyListener(new KeyListener() {
			public void keyReleased(KeyEvent e) {
				// TODO 自动生成的方法存根
				char keychar=e.getKeyChar();
				System.out.print(keychar);
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO 自动生成的方法存根
				
			}});
		panel.add(j1);
		panel.add(passwordField);
		this.add(panel);
		JButton button=new JButton("登录");
		button.setFont(new Font("楷体",Font.BOLD,18));
		button.setForeground(Color.blue);
		JButton button1=new JButton("注册");
		button1.setFont(new Font("楷体",Font.BOLD,18));
		button1.setForeground(Color.blue);
		JButton button2=new JButton("取消");
		button2.setFont(new Font("楷体",Font.BOLD,18));
		button2.setForeground(Color.blue);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string=field.getText();
				String string2=new String(passwordField.getPassword());
				if((!string.matches("\\w+\\d+"))||(!string2.matches("\\w+\\d+")))
				{
					JOptionPane.showMessageDialog(null,"密码和账号只能由数字和字母组成","登录提示",JOptionPane.ERROR_MESSAGE);
				}
				else {
					JOptionPane.showMessageDialog(null, "登陆成功","登录提示",JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		button1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string=field.getText();
				String string2=new String(passwordField.getPassword());
				if((!string.matches("\\w+\\d+"))||(!string2.matches("\\w+\\d+")))
				{
					JOptionPane.showMessageDialog(null,"密码和账号只能由数字和字母组成","注册提示",JOptionPane.ERROR_MESSAGE);
				}
				else {
					JOptionPane.showMessageDialog(null, "注册成功","注册提示",JOptionPane.INFORMATION_MESSAGE);
					dispose();
				}
			}
		});
		button2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				dispose();
			}
		});
		panel.add(button);
		panel.add(button1);
		panel.add(button2);
	}
}
public class gui2 {
	public static void main(String[] args)
	{
		new Demo().setVisible(true);
	}
} 